<?php
header("Content-Type:text/html;charset=utf-8");

function notice($msg = '', $url = '')
{
    if(empty($url))
    {
        echo "<script>alert('$msg');history.go(-1)</script>";
        exit;
    }else
    {
        echo "<script>alert('$msg');location.href = '$url'</script>";
        exit;
    }
}

function delete($dir)
{
    $list = scandir($dir);

    foreach($list as $item)
    {
        if($item == "." || $item == "..")
        {
            continue;
        }

        $current = $dir.'/'.$item;

        if(is_file($current))
        {
            @unlink($current);
        }

        if(is_dir($current))
        {
            delete($current);
        }
    }

    return rmdir($dir);
}

function upload($name, $path)
{
    $file = $_FILES[$name];

    $error = $file['error'];

    if($error > 0)
    {
        switch($error)
        {
            case 1:
                echo "文件上传的大小已超过系统的设置";
                break;
            case 2:
                echo "文件上传的大小已经超过了表单的限制";
                break;
            case 3:
                echo "网络中断";
                break;
            default:
                echo "其他未知的错误";
                break;
        }
    }

    $name = pathinfo($file['name'], PATHINFO_FILENAME);
    $ext = pathinfo($file['name'], PATHINFO_EXTENSION);

    $filename = $name."-".date("YmdHis", time()).".".$ext;

    $filename = $path.'/'.$filename;

    if(is_uploaded_file($file['tmp_name']))
    {
        return move_uploaded_file($file['tmp_name'], $filename);
    }else 
    {
        return false;
    }
}